package br.unifor.mia.graph.setorcensitario;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Transient;

@Entity
public class SectorLink {

	@Id
	private String name;
	
	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	private Node tailSector;
	
	@ManyToOne(optional = false, fetch = FetchType.LAZY)
	private Node headSector;
	
	@OneToMany(mappedBy = "userInfoId.sectorLink", cascade = CascadeType.ALL)
	private Set<UserInfo> userInfo;

	@Transient
	private int totalCrimesInTailSector = 0;

	@Transient
	private int totalCrimesInHeadSector = 0;
	
	/**
	 * Default Constructor, used by Hibernate.
	 */
	public SectorLink() {
		// Default Constructor, used by Hibernate.
	}
	
	public SectorLink(String name, Node tailSector, Node headSector) {
		this.name = name;
		this.setTailSector(tailSector);
		this.setHeadSector(headSector);
	}

	public String getName() {
		return name;
	}

	public Node getTailSector() {
		return tailSector;
	}

	private void setTailSector(Node tailSector) {
		this.tailSector = tailSector;
	}

	public Node getHeadSector() {
		return headSector;
	}

	private void setHeadSector(Node headSector) {
		this.headSector = headSector;
	}

	public Set<UserInfo> getUserInfo() {
		return userInfo;
	}

	/**
	 * Conjunto de usuarios que registram crimes no headSector E tailSector.
	 * 
	 * @return
	 */
	private Set<Node> getUsers() {
		
		Set<Node> headSectorUsers = new HashSet<Node>();
		
		for (Link link : headSector.getSectorLinks()) {
			headSectorUsers.add(link.getUser());
		}
		
		Set<Node> users = new HashSet<Node>();
		
		for (Link link : tailSector.getSectorLinks()) {
			if (headSectorUsers.contains(link.getUser())) {
				users.add(link.getUser());
			}
		}

		return users;
	}

	public void inicializeInfoUserSet() {
		
		totalCrimesInHeadSector = 0;
		totalCrimesInTailSector = 0;
		
		Set<Node> users = getUsers();
		
		for (Node user : users) {
			totalCrimesInTailSector += tailSector.getNumberOfCrimesOf(user);
			totalCrimesInHeadSector += headSector.getNumberOfCrimesOf(user);
			
			UserInfo userInfo = new UserInfo(user.getName(), this, tailSector.getNumberOfCrimesOf(user), headSector.getNumberOfCrimesOf(user));
			getUserInfo().add(userInfo);
		}
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof SectorLink)) {
			return false;
		}
		SectorLink other = (SectorLink) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "SectorLink [" + name + "]";
	}
}
